package com.yxhl.zoume.core.func.pay.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.yxhl.zoume.ExpressCarBillDetailFragmentDataBinding;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseDataBindingFragment;
import com.yxhl.zoume.core.func.pay.activity.CarPaymentActivity;
import com.yxhl.zoume.core.func.pay.adapter.ExpressCarBillDetailAdapter;
import com.yxhl.zoume.core.func.pay.model.CarPaymentEntrance;
import com.yxhl.zoume.core.func.pay.model.ExpressCarBill;
import com.yxhl.zoume.core.func.pay.model.event.ExpressCarBillDetailEvent;
import com.yxhl.zoume.core.func.pay.presenter.ExpressCarBillDetailPresenter;
import com.yxhl.zoume.core.func.pay.view.ExpressCarBillDetailView;
import com.yxhl.zoume.di.component.pay.DaggerExpressCarBillDetailComponent;
import com.yxhl.zoume.di.component.pay.ExpressCarBillDetailComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.utils.PromptUtils;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

import static com.yxhl.zoume.core.func.webpage.info.WebPageEntrance.PRICE_RULES_DETAIL;
import static com.yxhl.zoume.core.func.webpage.info.WebPageEntrance.SPECIAL_CAR_PRICE_INTRODUCTION;

/**
 * Created by vhawk on 12/22/16.
 */
public class ExpressCarBillDetailFragment extends BaseDataBindingFragment implements ExpressCarBillDetailView {

    private ExpressCarBillDetailFragmentDataBinding dataBinding;

    private static final String KEY_ORDER_ID = "ORDER_ID";
    private static final String KEY_TOTAL_FEE = "TOTAL_FEE";
    private static final String KEY_DISCOUNT_FEE = "DISCOUNT_FEE";
    private static final String KEY_PAYMENT_ENTRANCE = "PAYMENT_ENTRANCE";

    @Inject
    ExpressCarBillDetailPresenter billDetailPresenter;
    private String orderId;
    private String totalFee;
    private String discountFee;
    private ExpressCarBillDetailComponent component;

    @BindView(R.id.recycle_fee_detail)
    RecyclerView feeDetailRecycleView;

    @BindView(R.id.tv_express_car_price_rule)
    TextView priceRuler;
    private CarPaymentActivity carPaymentActivity;
    private CarPaymentEntrance paymentEntrance;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        carPaymentActivity = (CarPaymentActivity) this.mActivity;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        handleExtraParam();
        billDetailPresenter.attachView(this);
        billDetailPresenter.fetchBillDetail(orderId, totalFee, discountFee);
        onClickEvent();
    }

    private void onClickEvent() {
        RxView.clicks(priceRuler).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                switch (paymentEntrance) {
                    case CAR_PAYMENT_ENTRANCE_SPECIAL_CAR_SHARE:
                    case CAR_PAYMENT_ENTRANCE_SPECIAL_CAR_CHARTER:
                        getAppComponent().navigator().navigateToWebPageActivity((CarPaymentActivity) component.context(), SPECIAL_CAR_PRICE_INTRODUCTION);
                        break;

                    case CAR_PAYMENT_ENTRANCE_EXPRESS_CAR_TIME:
                    case CAR_PAYMENT_ENTRANCE_EXPRESS_CAR_WAY:
                        getAppComponent().navigator().navigateToWebPageActivity((CarPaymentActivity) component.context(), PRICE_RULES_DETAIL);

                        break;
                }
            }
        });
    }

    private void handleExtraParam() {
        Bundle bundle = getArguments();
        orderId = bundle.getString(KEY_ORDER_ID);
        totalFee = bundle.getString(KEY_TOTAL_FEE);
        discountFee = bundle.getString(KEY_DISCOUNT_FEE);
        paymentEntrance = (CarPaymentEntrance) bundle.getSerializable(KEY_PAYMENT_ENTRANCE);
    }


    @Override
    protected void initializeInjector() {
        component = DaggerExpressCarBillDetailComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(getActivity()))
                .build();
        component.inject(this);
    }

    @Override
    protected View getContentLayout(LayoutInflater inflater, ViewGroup container) {
        dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_pay_express_car_bill_detail, container, false);
        return dataBinding.getRoot();
    }

    @Override
    protected void setupToolBar() {

    }

    public static Fragment newInstance(ExpressCarBillDetailEvent event, CarPaymentEntrance carPaymentEntrance) {
        ExpressCarBillDetailFragment fragment = new ExpressCarBillDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_ORDER_ID, event.getOrderSerial());
        bundle.putString(KEY_TOTAL_FEE, event.getTotalFee());
        bundle.putString(KEY_DISCOUNT_FEE, event.getDiscountFee());
        bundle.putSerializable(KEY_PAYMENT_ENTRANCE, carPaymentEntrance);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void handleBillDetailSucceed(ExpressCarBill billInfo) {
        dataBinding.setExpressCarBill(billInfo);
        ExpressCarBillDetailAdapter adapter = new ExpressCarBillDetailAdapter(billInfo.getCarBillDetailList());
        feeDetailRecycleView.setLayoutManager(new LinearLayoutManager(component.context()));
        feeDetailRecycleView.setAdapter(adapter);
    }

    @Override
    public void handleBillDetailFailure(String msg) {
        PromptUtils.showShort(component.context(), msg);
    }

    @Override
    public void handleBillDetailNetError() {
        PromptUtils.showShort(component.context(), R.string.net_error);
    }


    @Override
    public void onResume() {
        super.onResume();
        ((CarPaymentActivity) component.context()).setToolbarTitle("行程费用明细");
        carPaymentActivity.showLeftIcon();
    }

    @Override
    public void onPause() {
        super.onPause();
        carPaymentActivity.hideLeftIcon();
    }

}
